/*
 * Copyright (C) 2022 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

import QtQuick 2.12
import org.ukui.tablet 1.0
import "./baseComponent"

LauncherGridViewDelegateDropArea {
    id: delegateContent
    width: modelData.columnSpan *  Style.appIconGridWidth
    height: modelData.rowSpan *  Style.appIconGridHeight
    enabled: !GroupController.groupPopuped

    property int itemId: modelData.id
    property bool mouseAreaReleased: false
    property int type: modelData.type

    Binding { target: itemRoot; property: "itemId"; value: itemId }
    Component.onCompleted: {
        itemRoot.visualIndex = index
    }

    Timer {
        id: popUpTimer
        property int dragSourceId: 0
        property int timeOutCount: 0
        interval: 600
        repeat: true

        onTriggered: {
            ++ timeOutCount
            if (timeOutCount == 1) {
                modelManager.setItemState(LauncherItem.Under, modelData.id)
                GroupController.aboutToInsertItemToGroup(itemRoot, drag.source)
            }
            if (timeOutCount >= 2) {
                if (drag.source !== null) {
                    GroupController.popupGroup(itemRoot.launcherViewAppIcon, true)
                    //删除桌面占位符
                    modelManager.removePlaceholderItem()
                    popUpTimer.stop()
                }
            }
            taskbar.taskbarReplayAllowed = false
        }
        onRunningChanged: {
            if (!running) {
                timeOutCount = 0
            }
        }
    }

    onEntered: {
        if (!dragEnable ||
                (modelData.type !== LauncherItem.AppIcon && modelData.type !== LauncherItem.Group) ||
                drag.source.visualIndex === visualIndex || drag.source.appOpen ||
                drag.source.itemId === undefined || drag.source.type === LauncherItem.Group ||
                drag.source.type === LauncherItem.Widget) {
            return
        }
        popUpTimer.start()
    }
    onExited: {
        if (!GroupController.groupPopuped) {
            // 鼠标release事件也会导致进入onExited
            if (!mouseAreaReleased) {
                GroupController.canceInsertToGroup()
                modelManager.setItemState(LauncherItem.Editable, modelData.id)
                popUpTimer.stop()
            } else {
                mouseAreaReleased = false
            }
        }
    }
    DropArea {
        id: moveDropArea
        width: parent.width-100 * Style.tabletScaleFactor//modelData.columnSpan * 80 + (modelData.columnSpan-1) * 160
        height: parent.height-60 * Style.tabletScaleFactor//modelData.rowSpan * 80 + (modelData.rowSpan-1) * 114

        anchors.centerIn: parent
        onEntered: {
            if (!dragEnable || modelData.type === LauncherItem.AddIcon /*||
                    drag.source.groupId !== root.groupId || drag.source.visualIndex === visualIndex*/) {
                return
            }
            if (drag.source.visualIndex === undefined || drag.source.visualIndex === -1 ||
                    drag.source.appOpen) {
                return
            }
            popUpTimer.stop()
            if (drag.source.pageIndex !== pageIndex || drag.source.groupId !== root.groupId) {
                modelManager.addPlaceholderItem(visualIndex, pageIndex, drag.source.rowSpan, drag.source.columnSpan)
            } else {
                modelManager.moveItem(drag.source.visualIndex, visualIndex, pageIndex)
                modelManager.removePlaceholderItem();
            }
        }
        onExited: {
            modelManager.setItemState(LauncherItem.Editable, modelData.id)
        }
    }

    TabletLauncherGridViewDelegateItem {
        id: itemRoot
    }
}
